package com.itheima.controller;

import com.itheima.pojo.Brand;
import com.itheima.service.BrandService;
import com.itheima.service.impl.BrandServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@WebServlet("/brand")
public class BrandController extends HttpServlet {
    private BrandService brandService = new BrandServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        String action = req.getParameter("action");
        switch (action) {
            case "add":
                this.add(req, resp);
                break;
            case "delect":
                this.delectById(req, resp);
                break;
            case "update":
                this.update(req, resp);
                break;
            case "findById":
                this.findById(req, resp);
                break;
            case "findAll":
                this.findAll(req, resp);
        }
    }

    /**
     * 查找全部
     *
     * @param req
     * @param resp
     */
    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        List<Brand> brands = brandService.findAll();
        req.setAttribute("brands", brands);
        try {
            req.getRequestDispatcher("/bandList.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过id查找
     *
     * @param req
     * @param resp
     */
    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        Brand brand = brandService.findById(id);
        req.setAttribute("brand", brand);
        try {
            req.getRequestDispatcher("/updateBrand.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改brand
     *
     * @param req
     * @param resp
     */
    private void update(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            Brand brand = new Brand();
            BeanUtils.populate(brand, parameterMap);
            brandService.update(brand);
            this.findAll(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 通过id删除
     *
     * @param req
     * @param resp
     */
    private void delectById(HttpServletRequest req, HttpServletResponse resp) {
        String id = req.getParameter("id");
        brandService.delectById(id);
        this.findAll(req, resp);
    }

    /**
     * 添加
     *
     * @param req
     * @param resp
     */
    private void add(HttpServletRequest req, HttpServletResponse resp) {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            Brand brand = new Brand();
            BeanUtils.populate(brand, parameterMap);
            brandService.add(brand);
            this.findAll(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
